package cn.com.czpb.client.module.subscribe.activity;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gyf.barlibrary.ImmersionBar;
import cn.com.czpb.client.R;
import cn.com.czpb.client.api.InterfaceJsonfile;
import cn.com.czpb.client.custorm.CircleImageView;
import cn.com.czpb.client.model.base.BaseEntity;
import cn.com.czpb.client.model.subscribe.FocusUserDetailBean;
import cn.com.czpb.client.model.subscribe.FocusUserDetailEntity;
import cn.com.czpb.client.model.subscribe.IsfocusUserEntity;
import cn.com.czpb.client.module.subscribe.RefreshSubScribeListListener;
import cn.com.czpb.client.module.subscribe.adapter.SubscribeFragmentPagerAdapter;
import cn.com.czpb.client.module.subscribe.fragment.SubscribeDetailFragment;
import cn.com.czpb.client.module.subscribe.fragment.SubscribeNewsListFragment;
import cn.com.czpb.client.net.Factory;
import cn.com.czpb.client.utils.DeviceUtils;
import cn.com.czpb.client.utils.SPUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by 猪猪侠 on 2017/8/11.
 */

public class SubscribeDetailActivity2 extends AppCompatActivity {
    @BindView(R.id.title_stilte_layout)
    TextView title_toolbar;
    //    @BindView(R.id.toolbar)
//    Toolbar toolbar;
    @BindView(R.id.rl_sub)
    RelativeLayout rl_sub;

    @BindView(R.id.img_icon_id)//头像
            CircleImageView imgIconId;
    @BindView(R.id.img_focususer_id)//订阅
            ImageView imgFocususerId;
    @BindView(R.id.tv_name_id)//name
            TextView tvNameId;
    @BindView(R.id.tv_describe_id)//介绍
            TextView tvDescribeId;
    @BindView(R.id.tv_subscribe_num_id)//订阅数量
            TextView tvSubscribeNumId;
    @BindView(R.id.tablayout_id)//tablayout
            TabLayout tablayoutId;
    @BindView(R.id.viewpager_id)//viewpager
            ViewPager viewpagerId;
    @BindView(R.id.img_removefocususer_id)
    ImageView imgRemovefocususerId;
    @BindView(R.id.share_subscribe)
    ImageView share_subscribe;
    private String focusId;
    private SubscribeFragmentPagerAdapter adapter;
    private String currentUid;
    private SPUtil spu;
    protected ImmersionBar mImmersionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subscribe_detail);
        ButterKnife.bind(this);
        spu = SPUtil.getInstance();
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
        initView_1();

        mImmersionBar.statusBarView(R.id.top_view)
                .navigationBarColor(R.color.transparent)
                .fullScreen(true)
                .statusBarDarkFont(true)
                .init();
    }

    public void initView_1() {
        focusId = getIntent().getStringExtra("focusUid");
        if (spu.getUser() != null) {
            if (focusId.equals(spu.getUser().getUid())) {
                rl_sub.setVisibility(View.GONE);
            }
        }
        share_subscribe.setVisibility(View.GONE);
//        toolbar.setBackgroundResource(R.drawable.black_background);
        adapter = new SubscribeFragmentPagerAdapter(getSupportFragmentManager());
        List<Fragment> list = new ArrayList<>();
        list.add(new SubscribeNewsListFragment());
        list.add(SubscribeDetailFragment.getIntance(focusId));
        adapter.setFragmentList(list);
        viewpagerId.setAdapter(adapter);
        tablayoutId.setupWithViewPager(viewpagerId);
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }

        getFocusUserDetail(focusId);
    }

    //设置状态
    private void setFocusData(String status) {
        if ("0".equals(status)) {
            imgFocususerId.setVisibility(View.VISIBLE);
            imgRemovefocususerId.setVisibility(View.GONE);
        } else {
            imgFocususerId.setVisibility(View.GONE);
            imgRemovefocususerId.setVisibility(View.VISIBLE);
        }
    }

    private void getFocusUserDetail(String focusId) {
        Map<String, String> map = new HashMap();
        map.put("siteid", InterfaceJsonfile.SITEID);
        map.put("uid", focusId);
        if (spu.getUser() != null) {
            map.put("nowuid", spu.getUser().getUid());
        }
        map.put("device", DeviceUtils.getMyUUID(this));
        Factory.provideHttpService().focusUserDetail(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<FocusUserDetailEntity>() {
                    @Override
                    public void call(FocusUserDetailEntity entity) {
                        setData(entity.data);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                    }
                });
    }

    public void setData(FocusUserDetailBean bean) {
        currentUid = bean.getUid();
        isFocus(bean.getUid());
        Glide.with(this).load(bean.getAvatarPath()).crossFade().placeholder(R.drawable.default_bg).into(imgIconId);
        tvNameId.setText(bean.getNickname());
        tvDescribeId.setText("介绍:" + bean.getDesc());
        tvSubscribeNumId.setText(bean.getFans());
        RefreshSubScribeListListener refreshSubScribeListListener = (RefreshSubScribeListListener) adapter.getItem(0);
        RefreshSubScribeListListener refreshSubScribeListListener1 = (RefreshSubScribeListListener) adapter.getItem(1);
        refreshSubScribeListListener.refresh(bean);
        refreshSubScribeListListener1.refresh(bean);

    }

    @OnClick({R.id.img_focususer_id, R.id.img_removefocususer_id})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_focususer_id:
                focusUser(currentUid);
                break;
            case R.id.img_removefocususer_id:
                removeFocusUser(currentUid);
                break;
        }
    }

    @OnClick(R.id.back_stitle_layout)
    public void goback(View view) {
        finish();
    }

    //订阅达人号
    private void focusUser(String uid) {
        Map<String, String> map = new HashMap<>();
        if (spu.getUser() != null) {
            map.put("uid", spu.getUser().getUid());
        }
        map.put("device", DeviceUtils.getMyUUID(this));
        map.put("focusid", uid);
        Factory.provideHttpService().focusUser(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseEntity>() {
                    @Override
                    public void call(BaseEntity baseEntity) {
                        isFocus(currentUid);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });

    }

    //取消订阅
    private void removeFocusUser(String uid) {
        Map<String, String> map = new HashMap<>();
        if (spu.getUser() != null) {
            map.put("uid", spu.getUser().getUid());
        }
        map.put("device", DeviceUtils.getMyUUID(this));
        map.put("focusid", uid);
        Factory.provideHttpService().removeFocusUser(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseEntity>() {
                    @Override
                    public void call(BaseEntity baseEntity) {
                        isFocus(currentUid);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });

    }

    //判断是否订阅
    private void isFocus(String focusid) {
        HashMap<String, String> map = new HashMap<>();
        map.put("device", DeviceUtils.getMyUUID(this));
        map.put("focusid", focusid);
        if (spu.getUser() != null) {
            map.put("uid", spu.getUser().getUid());
        }
        Factory.provideHttpService().isFocusUser(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<IsfocusUserEntity>() {
                    @Override
                    public void call(IsfocusUserEntity isfocusUserEntity) {
                        setFocusData(isfocusUserEntity.data.getStatus());
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });
    }
}
